package com.qq.BFMRSE.model;

import java.io.Serializable;
import java.util.BitSet;

public class BloomFilter implements Serializable{
	private static final long serialVersionUID = 1L;
	private static final int DEFAULT_SIZE=1<<16;
	private static final int [] seeds=new int[]{7, 11, 13, 31, 37, 61};
	private  BitSet bits=new BitSet(DEFAULT_SIZE);
	private  SimpleHash[] hashFun=new SimpleHash[seeds.length];
	public BitSet getBits()
	{
		return bits;
	}
	public void setBits(BitSet bits)
	{
		this.bits=bits;
	}
	public BloomFilter()
	{
		for(int i=0;i<seeds.length;i++)
		{
			hashFun[i]=new SimpleHash(DEFAULT_SIZE, seeds[i]);
		}
	}
	/**
	 * ����bits�ĳ���
	 * @return
	 */
	public int BitsLength()
	{
		return DEFAULT_SIZE;
	}
	/**
	 * ��bloom filter�м���һ���ؼ���
	 * @param value
	 */
	public  void addValue(String value)
	{
		for(SimpleHash fun:hashFun)
		{
			bits.set(fun.Hash(value),true);
		}
	}
	/**
	 * �жϹؼ���valueʱ�����
	 * @param value
	 * @return
	 */
	public  boolean isContains(String value)
	{
		boolean flag=true;
		if(value==null)
		{
			flag=false;
		}
		else {
			for(SimpleHash fun:hashFun)
			{
				if(bits.get(fun.Hash(value))==false)
					flag=false;
			}
		}
		return flag;
	}
	public int hashCount()
	{
		return seeds.length;
	}
	
	
	

}
